{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c6979cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "#问题二附件4上网业务打分预测代码\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import classification_report\n",
    "from sklearn.model_selection import cross_val_score\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "from lightgbm import LGBMClassifier\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from xgboost import XGBClassifier\n",
    "from sklearn.multiclass import OneVsRestClassifier\n",
    "from sklearn.ensemble import VotingClassifier\n",
    "from sklearn.multiclass import OneVsOneClassifier\n",
    "from mlxtend.classifier import StackingClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b6fbeea",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_4 = pd.read_excel('./data/data_2处理后的数据.xlsx').drop('Unnamed: 0', axis = 1)\n",
    "df1 = data_4.copy()\n",
    "df1 = pd.get_dummies(df1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21c5d854",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.multioutput import ClassifierChain\n",
    "from sklearn.metrics import jaccard_score\n",
    "X_train, X_test, y_train, y_test = train_test_split(df1.iloc[:, 4 : ], df1.iloc[:, :4], test_size = 0.3, random_state = 10)\n",
    "model_1 = RandomForestClassifier()\n",
    "chains = [ClassifierChain(model_1, order='random', random_state=i)\n",
    "          for i in range(10)]\n",
    "for chain in chains:\n",
    "    chain.fit(X_train, y_train)\n",
    "Y_pred_chains = np.array([chain.predict(X_test) for chain in chains], dtype = 'int64')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "52197a61",
   "metadata": {},
   "outputs": [],
   "source": [
    "jac_scores = []\n",
    "for i in range(len(chains)):\n",
    "    for j in range(len(y_test.to_numpy())):\n",
    "        a = list(y_test.to_numpy()[j])\n",
    "        b = list(Y_pred_chains[i][j])\n",
    "        jac_score = jaccard_score(a, b, average = None)\n",
    "        jac_scores.append(jac_score[0])\n",
    "    sums = 0\n",
    "    for n in jac_scores:\n",
    "        sums += n\n",
    "    avg = sums / len(jac_scores)\n",
    "    print('第%i条链的平均Jaccard相似系数为:%.3f'%((i + 1), avg))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89d15eed",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(len(chains)):\n",
    "    y_pred = pd.DataFrame(Y_pred_chains[i])\n",
    "    scores = []\n",
    "    for j in range(4):\n",
    "        score = accuracy_score(y_test.iloc[:, j], y_pred.iloc[:, j])\n",
    "        scores.append(score)\n",
    "    print('第%i条链的准确率为:'%((i + 1)))\n",
    "    print(scores)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8abd3307",
   "metadata": {},
   "outputs": [],
   "source": [
    "model_3 = LGBMClassifier()\n",
    "chains = [ClassifierChain(model_3, order='random', random_state=i)\n",
    "          for i in range(10)]\n",
    "for chain in chains:\n",
    "    chain.fit(X_train, y_train)\n",
    "Y_pred_chains = np.array([chain.predict(X_test) for chain in chains], dtype = 'int64')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5cbbf3c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "jac_scores = []\n",
    "for i in range(len(chains)):\n",
    "    for j in range(len(y_test.to_numpy())):\n",
    "        a = list(y_test.to_numpy()[j])\n",
    "        b = list(Y_pred_chains[i][j])\n",
    "        jac_score = jaccard_score(a, b, average = None)\n",
    "        jac_scores.append(jac_score[0])\n",
    "    sums = 0\n",
    "    for n in jac_scores:\n",
    "        sums += n\n",
    "    avg = sums / len(jac_scores)\n",
    "    print('第%i条链的平均Jaccard相似系数为:%.3f'%((i + 1), avg))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7e9889c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(len(chains)):\n",
    "    y_pred = pd.DataFrame(Y_pred_chains[i])\n",
    "    scores = []\n",
    "    for j in range(4):\n",
    "        score = accuracy_score(y_test.iloc[:, j], y_pred.iloc[:, j])\n",
    "        scores.append(score)\n",
    "    print('第%i条链的准确率为:'%((i + 1)))\n",
    "    print(scores)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac936d9c",
   "metadata": {},
   "outputs": [],
   "source": [
    "cols = ['手机上网整体满意度', '2G驻留时长', '重定向次数', '网络信号差/没有信号', '手机上网速度慢', '上网过程中网络时断时续或时快时慢', \\\n",
    "    '居民小区', '打开网页或APP图片慢', '显示有信号上不了网', '办公室', '地铁', '看视频卡顿', '当月GPRS资源使用量（GB）', '今日头条使用流量', '快手使用流量', \\\n",
    "        '腾讯视频使用流量', '抖音使用流量（MB）', '视频类应用流量', '阿里系APP流量', '通信类应用流量', '优酷视频使用流量', '游戏类APP使用流量', \\\n",
    "            '本年累计消费（元）', '网页类应用流量', '年龄', '小视频系APP流量', '近3个月平均消费（元）', \\\n",
    "                '近3个月平均消费（剔除通信账户支付）', '游戏类APP使用次数', '天猫使用流量', '腾讯系APP流量', '脱网次数', '下载速度慢', '微信', \\\n",
    "                '打游戏延时大', '网易系APP流量', '音乐类应用流量', '手机支付较慢','全部网页或APP都慢', '当月MOU','王者荣耀使用次数', '商业街',\\\n",
    "                    '王者荣耀APP使用流量','高铁','全部都卡顿','套外流量（MB）','游戏类应用流量','淘宝','滴滴出行使用流量','京东','终端品牌',\\\n",
    "                        '大众点评使用流量','套外流量费（元）','农村','抖音','饿了么使用流量','王者荣耀','腾讯视频','百度','爱奇艺','主套餐档位',\\\n",
    "                            '优酷','高校','畅享套餐名称','全部游戏都卡顿','操作系统','和平精英','王者荣耀使用天数','新浪微博','游戏类APP使用天数',\\\n",
    "                                '客户星级标识','手机QQ','今日头条','王者荣耀质差次数','畅享套餐档位','其他，请注明']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2631c60b",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_3 = pd.DataFrame()\n",
    "for i in cols:\n",
    "    df_3[i] = data_4[i]\n",
    "df_3 = pd.get_dummies(df_3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "34266933",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.ensemble import VotingClassifier\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_3.iloc[:, 1 : ], df_3.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "clf1 = XGBClassifier(random_state=10)\n",
    "clf2 = RandomForestClassifier(random_state = 10)\n",
    "clf3 = LGBMClassifier(random_state = 10)\n",
    "clf4 = GradientBoostingClassifier(random_state = 10)\n",
    "eclf = VotingClassifier(estimators=[('xgb', clf1), ('rf', clf2), ('lgb', clf3), ('gbdt', clf4)], voting = 'hard')\n",
    "y_test = y_test - 1\n",
    "for clf, label in zip([clf1, clf2, clf3, clf4, eclf], ['XGBBoosting', 'Random Forest', 'LightGBM', 'GBDT', 'Ensemble']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring='accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "478a4dd2",
   "metadata": {},
   "outputs": [],
   "source": [
    "from mlxtend.classifier import StackingClassifier\n",
    "sclf = StackingClassifier(classifiers = [clf1, clf3], meta_classifier = clf2)\n",
    "for clf, label in zip([clf2, clf3, sclf], ['Random Forest', 'LightGBM', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c58c4a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_3.iloc[:, 1 : ], df_3.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10)\n",
    "m3 = GradientBoostingClassifier(random_state = 10)\n",
    "m4 = LGBMClassifier(random_state = 10)\n",
    "m5 = XGBClassifier(random_state = 10)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsRestClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs Rest Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['r'] + ['b'] * (len(scores))\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "06e503ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.multiclass import OneVsOneClassifier\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_3.iloc[:, 1 : ], df_3.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10)\n",
    "m3 = GradientBoostingClassifier(random_state = 10)\n",
    "m4 = LGBMClassifier(random_state = 10)\n",
    "m5 = XGBClassifier(random_state = 10)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsOneClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs One Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['r'] + ['b'] * (len(scores))\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cfded5b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 前百分之八十的特征\n",
    "cols = ['手机上网整体满意度', '2G驻留时长', '重定向次数', '网络信号差/没有信号', '手机上网速度慢', '上网过程中网络时断时续或时快时慢', \\\n",
    "    '居民小区', '打开网页或APP图片慢', '显示有信号上不了网', '办公室', '地铁', '看视频卡顿', '当月GPRS资源使用量（GB）', '今日头条使用流量', '快手使用流量', \\\n",
    "        '腾讯视频使用流量', '抖音使用流量（MB）', '视频类应用流量', '阿里系APP流量', '通信类应用流量', '优酷视频使用流量', '游戏类APP使用流量', \\\n",
    "            '本年累计消费（元）', '网页类应用流量', '年龄', '小视频系APP流量', '近3个月平均消费（元）', \\\n",
    "                '近3个月平均消费（剔除通信账户支付）', '游戏类APP使用次数', '天猫使用流量', '腾讯系APP流量', '脱网次数', '下载速度慢', '微信', \\\n",
    "                '打游戏延时大', '网易系APP流量', '音乐类应用流量', '手机支付较慢','全部网页或APP都慢', '当月MOU','王者荣耀使用次数', '商业街',\\\n",
    "                    '王者荣耀APP使用流量','高铁','全部都卡顿','套外流量（MB）','游戏类应用流量','淘宝','滴滴出行使用流量','京东','终端品牌',\\\n",
    "                        '大众点评使用流量','套外流量费（元）','农村','抖音','饿了么使用流量','王者荣耀','腾讯视频','百度','爱奇艺','主套餐档位']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee2da3f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_4 = pd.DataFrame()\n",
    "for i in cols:\n",
    "    df_4[i] = data_4[i]\n",
    "df_4 = pd.get_dummies(df_4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f527d7ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df_4.iloc[:, 1 : ], df_4.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "clf1 = XGBClassifier(random_state=10)\n",
    "clf2 = RandomForestClassifier(random_state = 10)\n",
    "clf3 = LGBMClassifier(random_state = 10)\n",
    "clf4 = GradientBoostingClassifier(random_state = 10)\n",
    "eclf = VotingClassifier(estimators=[('xgb', clf1), ('rf', clf2), ('lgb', clf3), ('gbdt', clf4)], voting = 'hard')\n",
    "y_test = y_test - 1\n",
    "for clf, label in zip([clf1, clf2, clf3, clf4, eclf], ['XGBBoosting', 'Random Forest', 'LightGBM', 'GBDT', 'Ensemble']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring='accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74e39ee7",
   "metadata": {},
   "outputs": [],
   "source": [
    "sclf = StackingClassifier(classifiers = [clf1, clf3], meta_classifier = clf2)\n",
    "for clf, label in zip([clf1, clf3, sclf], ['XGboost', 'LightGBM', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))\n",
    "\n",
    "\n",
    "sclf = StackingClassifier(classifiers = [clf2, clf3], meta_classifier = clf1)\n",
    "for clf, label in zip([clf2, clf3, sclf], ['Random Forest', 'LightGBM', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))\n",
    "\n",
    "\n",
    "sclf = StackingClassifier(classifiers = [clf1, clf2], meta_classifier = clf3)\n",
    "for clf, label in zip([clf1, clf2, sclf], ['XGboost', 'Random Forest', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9353e28b",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_4.iloc[:, 1 : ], df_4.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10)\n",
    "m3 = GradientBoostingClassifier(random_state = 10)\n",
    "m4 = LGBMClassifier(random_state = 10)\n",
    "m5 = XGBClassifier(random_state = 10)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsRestClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs Rest Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['r'] + ['b'] * (len(scores))\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "05d5ec17",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_4.iloc[:, 1 : ], df_4.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10)\n",
    "m3 = GradientBoostingClassifier(random_state = 10)\n",
    "m4 = LGBMClassifier(random_state = 10)\n",
    "m5 = XGBClassifier(random_state = 10)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsOneClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs One Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['r'] + ['b'] * (len(scores))\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec577b19",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 前50%的特征\n",
    "cols = ['手机上网整体满意度', '2G驻留时长', '重定向次数', '网络信号差/没有信号', '手机上网速度慢', '上网过程中网络时断时续或时快时慢', \\\n",
    "    '居民小区', '打开网页或APP图片慢', '显示有信号上不了网', '办公室', '地铁', '看视频卡顿', '当月GPRS资源使用量（GB）', '今日头条使用流量', '快手使用流量', \\\n",
    "        '腾讯视频使用流量', '抖音使用流量（MB）', '视频类应用流量', '阿里系APP流量', '通信类应用流量', '优酷视频使用流量', '游戏类APP使用流量', \\\n",
    "            '本年累计消费（元）', '网页类应用流量', '年龄', '小视频系APP流量', '近3个月平均消费（元）', \\\n",
    "                '近3个月平均消费（剔除通信账户支付）', '游戏类APP使用次数', '天猫使用流量', '腾讯系APP流量', '脱网次数', '下载速度慢', '微信', \\\n",
    "                '打游戏延时大', '网易系APP流量', '音乐类应用流量', '手机支付较慢']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a2e5d622",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_5 = pd.DataFrame()\n",
    "for i in cols:\n",
    "    df_5[i] = data_4[i]\n",
    "df_5 = pd.get_dummies(df_5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "658c483a",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df_5.iloc[:, 1 : ], df_5.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "clf1 = XGBClassifier(random_state=10)\n",
    "clf2 = RandomForestClassifier(random_state = 10)\n",
    "clf3 = LGBMClassifier(random_state = 10)\n",
    "clf4 = GradientBoostingClassifier(random_state = 10)\n",
    "eclf = VotingClassifier(estimators=[('xgb', clf1), ('rf', clf2), ('lgb', clf3), ('gbdt', clf4)], voting = 'hard')\n",
    "y_test = y_test - 1\n",
    "for clf, label in zip([clf1, clf2, clf3, clf4, eclf], ['XGBBoosting', 'Random Forest', 'LightGBM', 'GBDT', 'Ensemble']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring='accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "981c7284",
   "metadata": {},
   "outputs": [],
   "source": [
    "sclf = StackingClassifier(classifiers = [clf1, clf3], meta_classifier = clf2)\n",
    "for clf, label in zip([clf1, clf3, sclf], ['XGboost', 'LightGBM', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))\n",
    "\n",
    "\n",
    "sclf = StackingClassifier(classifiers = [clf2, clf3], meta_classifier = clf1)\n",
    "for clf, label in zip([clf2, clf3, sclf], ['Random Forest', 'LightGBM', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))\n",
    "\n",
    "\n",
    "sclf = StackingClassifier(classifiers = [clf1, clf2], meta_classifier = clf3)\n",
    "for clf, label in zip([clf1, clf2, sclf], ['XGboost', 'Random Forest', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cbcb7ee0",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_5.iloc[:, 1 : ], df_5.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10)\n",
    "m3 = GradientBoostingClassifier(random_state = 10)\n",
    "m4 = LGBMClassifier(random_state = 10)\n",
    "m5 = XGBClassifier(random_state = 10)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsRestClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs Rest Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['r'] + ['b'] * (len(scores))\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a583b8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_4.iloc[:, 1 : ], df_4.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10)\n",
    "m3 = GradientBoostingClassifier(random_state = 10)\n",
    "m4 = LGBMClassifier(random_state = 10)\n",
    "m5 = XGBClassifier(random_state = 10)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsOneClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs One Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['r'] + ['b'] * (len(scores))\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c996bac",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 前20%的特征\n",
    "cols = ['手机上网整体满意度', '2G驻留时长', '重定向次数', '网络信号差/没有信号', '手机上网速度慢', '上网过程中网络时断时续或时快时慢', \\\n",
    "    '居民小区', '打开网页或APP图片慢', '显示有信号上不了网', '办公室', '地铁', '看视频卡顿', '当月GPRS资源使用量（GB）', '今日头条使用流量', '快手使用流量']\n",
    "df_6 = pd.DataFrame()\n",
    "for i in cols:\n",
    "    df_6[i] = data_4[i]\n",
    "df_6 = pd.get_dummies(df_6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c4dc663",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df_6.iloc[:, 1 : ], df_6.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "clf1 = XGBClassifier(random_state=10)\n",
    "clf2 = RandomForestClassifier(random_state = 10)\n",
    "clf3 = LGBMClassifier(random_state = 10)\n",
    "clf4 = GradientBoostingClassifier(random_state = 10)\n",
    "eclf = VotingClassifier(estimators=[('xgb', clf1), ('rf', clf2), ('lgb', clf3), ('gbdt', clf4)], voting = 'hard')\n",
    "y_test = y_test - 1\n",
    "for clf, label in zip([clf1, clf2, clf3, clf4, eclf], ['XGBBoosting', 'Random Forest', 'LightGBM', 'GBDT', 'Ensemble']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring='accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe0651ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "sclf = StackingClassifier(classifiers = [clf1, clf3], meta_classifier = clf2)\n",
    "for clf, label in zip([clf1, clf3, sclf], ['XGboost', 'LightGBM', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))\n",
    "\n",
    "\n",
    "sclf = StackingClassifier(classifiers = [clf2, clf3], meta_classifier = clf1)\n",
    "for clf, label in zip([clf2, clf3, sclf], ['Random Forest', 'LightGBM', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))\n",
    "\n",
    "\n",
    "sclf = StackingClassifier(classifiers = [clf1, clf2], meta_classifier = clf3)\n",
    "for clf, label in zip([clf1, clf2, sclf], ['XGboost', 'Random Forest', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d77f643d",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_6.iloc[:, 1 : ], df_6.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10)\n",
    "m3 = GradientBoostingClassifier(random_state = 10)\n",
    "m4 = LGBMClassifier(random_state = 10)\n",
    "m5 = XGBClassifier(random_state = 10)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsRestClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs Rest Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['r'] + ['b'] * (len(scores))\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e2374100",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_6.iloc[:, 1 : ], df_6.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10)\n",
    "m3 = GradientBoostingClassifier(random_state = 10)\n",
    "m4 = LGBMClassifier(random_state = 10)\n",
    "m5 = XGBClassifier(random_state = 10)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsOneClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs One Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['r'] + ['b'] * (len(scores))\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ff228b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 过采样\n",
    "cols = ['手机上网整体满意度', '2G驻留时长', '重定向次数', '网络信号差/没有信号', '手机上网速度慢', '上网过程中网络时断时续或时快时慢', \\\n",
    "    '居民小区', '打开网页或APP图片慢', '显示有信号上不了网', '办公室', '地铁', '看视频卡顿', '当月GPRS资源使用量（GB）', '今日头条使用流量', '快手使用流量', \\\n",
    "        '腾讯视频使用流量', '抖音使用流量（MB）', '视频类应用流量', '阿里系APP流量', '通信类应用流量', '优酷视频使用流量', '游戏类APP使用流量', \\\n",
    "            '本年累计消费（元）', '网页类应用流量', '年龄', '小视频系APP流量', '近3个月平均消费（元）', \\\n",
    "                '近3个月平均消费（剔除通信账户支付）', '游戏类APP使用次数', '天猫使用流量', '腾讯系APP流量', '脱网次数', '下载速度慢', '微信', \\\n",
    "                '打游戏延时大', '网易系APP流量', '音乐类应用流量', '手机支付较慢']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b9cd972f",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_7 = pd.DataFrame()\n",
    "for i in cols:\n",
    "    df_7[i] = data_4[i]\n",
    "df_7 = pd.get_dummies(df_7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb3ce5bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = df_7.iloc[:, 1 :]\n",
    "y = df_7.iloc[:, 0 ]\n",
    "smote_model = SMOTE()\n",
    "x_smote, y_smote = smote_model.fit_resample(x, y)\n",
    "smote_df = pd.concat([y_smote, x_smote], axis=1)\n",
    "df = smote_df.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25523177",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "clf1 = XGBClassifier(random_state=10)\n",
    "clf2 = RandomForestClassifier(random_state = 10)\n",
    "clf3 = LGBMClassifier(random_state = 10)\n",
    "eclf = VotingClassifier(estimators=[('xgb', clf1), ('rf', clf2), ('lgb', clf3)], voting = 'hard')\n",
    "y_test = y_test - 1\n",
    "for clf, label in zip([clf1, clf2, clf3, clf4, eclf], ['XGBBoosting', 'Random Forest', 'LightGBM', 'Ensemble']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring='accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b03bab1e",
   "metadata": {},
   "outputs": [],
   "source": [
    "sclf = StackingClassifier(classifiers = [clf1, clf3], meta_classifier = clf2)\n",
    "for clf, label in zip([clf1, clf3, sclf], ['XGboost', 'LightGBM', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))\n",
    "\n",
    "\n",
    "sclf = StackingClassifier(classifiers = [clf2, clf3], meta_classifier = clf1)\n",
    "for clf, label in zip([clf2, clf3, sclf], ['Random Forest', 'LightGBM', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))\n",
    "\n",
    "\n",
    "sclf = StackingClassifier(classifiers = [clf1, clf2], meta_classifier = clf3)\n",
    "for clf, label in zip([clf1, clf2, sclf], ['XGboost', 'Random Forest', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "309914f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10)\n",
    "m3 = GradientBoostingClassifier(random_state = 10)\n",
    "m4 = LGBMClassifier(random_state = 10)\n",
    "m5 = XGBClassifier(random_state = 10)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsRestClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs Rest Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['b'] * (len(scores) - 1) + ['r']\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac1cae47",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10)\n",
    "m3 = GradientBoostingClassifier(random_state = 10)\n",
    "m4 = LGBMClassifier(random_state = 10)\n",
    "m5 = XGBClassifier(random_state = 10)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsOneClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs One Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['b'] * (len(scores) - 1) + ['r']\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad3eee24",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 预测阶段\n",
    "model = OneVsRestClassifier(RandomForestClassifier(random_state = 10))\n",
    "model.fit(X_train, y_train)\n",
    "print(classification_report(y_test, model.predict(X_test)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d9d41a3a",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_excel('./附件4上网业务用户满意度预测数据.xlsx')\n",
    "data = data.drop('用户id', axis = 1)\n",
    "data['网络信号差/没有信号'].replace(-1, '没有', inplace = True)\n",
    "data['网络信号差/没有信号'].replace(1, '有', inplace = True)\n",
    "data['手机上网速度慢'].replace(-1, '没有', inplace = True)\n",
    "data['手机上网速度慢'].replace(4, '有', inplace = True)\n",
    "data['上网过程中网络时断时续或时快时慢'].replace(-1, '没有', inplace = True)\n",
    "data['上网过程中网络时断时续或时快时慢'].replace(3, '有', inplace = True)\n",
    "data['居民小区'].replace(-1, '不在', inplace = True)\n",
    "data['居民小区'].replace(1, '在', inplace = True)\n",
    "data['打开网页或APP图片慢'].replace(-1, '没有', inplace = True)\n",
    "data['打开网页或APP图片慢'].replace(3, '有', inplace = True)\n",
    "data['显示有信号上不了网'].replace(-1, '没有', inplace = True)\n",
    "data['显示有信号上不了网'].replace(2, '有', inplace = True)\n",
    "data['办公室'].replace(-1, '不在', inplace = True)\n",
    "data['办公室'].replace(2, '在', inplace = True)\n",
    "data['地铁'].replace(-1, '不在', inplace = True)\n",
    "data['地铁'].replace(5, '在', inplace = True)\n",
    "data['看视频卡顿'].replace(-1, '没有', inplace = True)\n",
    "data['看视频卡顿'].replace(1, '有', inplace = True)\n",
    "data['下载速度慢'].replace(-1, '没有', inplace = True)\n",
    "data['下载速度慢'].replace(4, '有', inplace = True)\n",
    "data['打游戏延时大'].replace(-1, '没有', inplace = True)\n",
    "data['打游戏延时大'].replace(2, '有', inplace = True)\n",
    "data['手机支付较慢'].replace(-1, '没有', inplace = True)\n",
    "data['手机支付较慢'].replace(5, '有', inplace = True)\n",
    "data['微信'].replace(-1, '没有', inplace = True)\n",
    "data['微信'].replace(1, '有', inplace = True)\n",
    "data['全部网页或APP都慢'].replace(-1, '否', inplace = True)\n",
    "data['全部网页或APP都慢'].replace(99, '是', inplace = True)\n",
    "data['商业街'].replace(-1, '不在', inplace = True)\n",
    "data['商业街'].replace(4, '在', inplace = True)\n",
    "data['高铁'].replace(-1, '不在', inplace = True)\n",
    "data['高铁'].replace(7, '在', inplace = True)\n",
    "data['全部都卡顿'].replace(-1, '否', inplace = True)\n",
    "data['全部都卡顿'].replace(99, '是', inplace = True)\n",
    "data['淘宝'].replace(-1, '没有', inplace = True)\n",
    "data['淘宝'].replace(3, '有', inplace = True)\n",
    "data['京东'].replace(-1, '没有', inplace = True)\n",
    "data['京东'].replace(4, '有', inplace = True)\n",
    "data['农村'].replace(-1, '不在', inplace = True)\n",
    "data['农村'].replace(6, '在', inplace = True)\n",
    "data['抖音'].replace(-1, '没有', inplace = True)\n",
    "data['抖音'].replace(6, '有', inplace = True)\n",
    "data['王者荣耀'].replace(-1, '没有', inplace = True)\n",
    "data['王者荣耀'].replace(2, '有', inplace = True)\n",
    "data['腾讯视频'].replace(-1, '没有', inplace = True)\n",
    "data['腾讯视频'].replace(3, '有', inplace = True)\n",
    "data['百度'].replace(-1, '没有', inplace = True)\n",
    "data['百度'].replace(5, '有', inplace = True)\n",
    "data['爱奇艺'].replace(-1, '没有', inplace = True)\n",
    "data['爱奇艺'].replace(1, '有', inplace = True)\n",
    "data['优酷'].replace(-1, '没有', inplace = True)\n",
    "data['优酷'].replace(2, '有', inplace = True)\n",
    "data['高校'].replace(-1, '不在', inplace = True)\n",
    "data['高校'].replace(3, '在', inplace = True)\n",
    "data['全部游戏都卡顿'].replace(-1, '否', inplace = True)\n",
    "data['全部游戏都卡顿'].replace(99, '是', inplace = True)\n",
    "data['和平精英'].replace(-1, '没有', inplace = True)\n",
    "data['和平精英'].replace(1, '有', inplace = True)\n",
    "data['新浪微博'].replace(-1, '没有', inplace = True)\n",
    "data['新浪微博'].replace(7, '有', inplace = True)\n",
    "data['手机QQ'].replace(-1, '没有', inplace = True)\n",
    "data['手机QQ'].replace(2, '有', inplace = True)\n",
    "data['今日头条'].replace(-1, '没有', inplace = True)\n",
    "data['今日头条'].replace(6, '有', inplace = True)\n",
    "data['其他，请注明'].replace(-1, '没有', inplace = True)\n",
    "data['其他，请注明'].replace(98, '有', inplace = True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1377f81b",
   "metadata": {},
   "outputs": [],
   "source": [
    "cols = ['网络信号差/没有信号', '手机上网速度慢', '上网过程中网络时断时续或时快时慢', '居民小区', '打开网页或APP图片慢', '显示有信号上不了网', '办公室', '地铁', \\\n",
    "    '看视频卡顿', '脱网次数', '下载速度慢', '微信', '打游戏延时大', '手机支付较慢', '全部网页或APP都慢', '当月MOU', '商业街', '高铁', '全部都卡顿', '套外流量（MB）', \\\n",
    "        '淘宝', '京东', '套外流量费（元）', '农村', '抖音', '王者荣耀', '腾讯视频', '百度', '爱奇艺', '优酷', '高校', '全部游戏都卡顿', '和平精英', '新浪微博', \\\n",
    "            '手机QQ', '今日头条', '其他，请注明']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b49ec245",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_8 = pd.DataFrame()\n",
    "for i in cols:\n",
    "    df_8[i] = data[i]\n",
    "df_8 = pd.get_dummies(df_8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "588988d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "cols = ['网络信号差/没有信号', '手机上网速度慢', '上网过程中网络时断时续或时快时慢', '居民小区', '打开网页或APP图片慢', '显示有信号上不了网', '办公室', '地铁', \\\n",
    "    '看视频卡顿', '脱网次数', '下载速度慢', '微信', '打游戏延时大', '手机支付较慢', '全部网页或APP都慢', '当月MOU', '商业街', '高铁', '全部都卡顿', '套外流量（MB）', \\\n",
    "        '淘宝', '京东', '套外流量费（元）', '农村', '抖音', '王者荣耀', '腾讯视频', '百度', '爱奇艺', '优酷', '高校', '全部游戏都卡顿', '和平精英', '新浪微博', \\\n",
    "            '手机QQ', '今日头条', '其他，请注明']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b351d9d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_9 = pd.DataFrame()\n",
    "for i in cols:\n",
    "    df_9[i] = data_4[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50a9b556",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_9['网络信号差/没有信号'].replace('不在', '没有', inplace = True)\n",
    "df_9['网络信号差/没有信号'].replace('在', '有', inplace = True)\n",
    "df_9['手机上网速度慢'].replace('不在', '没有', inplace = True)\n",
    "df_9['手机上网速度慢'].replace('在', '有', inplace = True)\n",
    "df_9['上网过程中网络时断时续或时快时慢'].replace('不在', '没有', inplace = True)\n",
    "df_9['上网过程中网络时断时续或时快时慢'].replace('在', '有', inplace = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f282782c",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_9['居民小区'].replace(-1, '不在', inplace = True)\n",
    "df_9['居民小区'].replace(1, '在', inplace = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95cd780d",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_9['打开网页或APP图片慢'].replace('不在', '没有', inplace = True)\n",
    "df_9['打开网页或APP图片慢'].replace('在', '有', inplace = True)\n",
    "df_9['显示有信号上不了网'].replace('不在', '没有', inplace = True)\n",
    "df_9['显示有信号上不了网'].replace('在', '有', inplace = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ca6166a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_9['办公室'].replace(-1, '不在', inplace = True)\n",
    "df_9['办公室'].replace(2, '在', inplace = True)\n",
    "df_9['地铁'].replace(-1, '不在', inplace = True)\n",
    "df_9['地铁'].replace(5, '在', inplace = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81fa0e29",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_9['看视频卡顿'].replace('不在', '没有', inplace = True)\n",
    "df_9['看视频卡顿'].replace('在', '有', inplace = True)\n",
    "df_9['下载速度慢'].replace('不在', '没有', inplace = True)\n",
    "df_9['下载速度慢'].replace('在', '有', inplace = True)\n",
    "df_9['打游戏延时大'].replace('不在', '没有', inplace = True)\n",
    "df_9['打游戏延时大'].replace('在', '有', inplace = True)\n",
    "df_9['手机支付较慢'].replace('不在', '没有', inplace = True)\n",
    "df_9['手机支付较慢'].replace('在', '有', inplace = True)\n",
    "df_9['微信'].replace('不在', '没有', inplace = True)\n",
    "df_9['微信'].replace('在', '有', inplace = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "90e397dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_9['全部网页或APP都慢'].replace(-1, '否', inplace = True)\n",
    "df_9['全部网页或APP都慢'].replace(99, '是', inplace = True)\n",
    "\n",
    "df_9['商业街'].replace(-1, '不在', inplace = True)\n",
    "df_9['商业街'].replace(4, '在', inplace = True)\n",
    "\n",
    "df_9['高铁'].replace(-1, '不在', inplace = True)\n",
    "df_9['高铁'].replace(7, '在', inplace = True)\n",
    "\n",
    "df_9['全部都卡顿'].replace(-1, '否', inplace = True)\n",
    "df_9['全部都卡顿'].replace(99, '是', inplace = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "115496b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_9['淘宝'].replace('不在', '没有', inplace = True)\n",
    "df_9['淘宝'].replace('在', '有', inplace = True)\n",
    "df_9['京东'].replace('不在', '没有', inplace = True)\n",
    "df_9['京东'].replace('在', '有', inplace = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77f3f5cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_9['农村'].replace(-1, '不在', inplace = True)\n",
    "df_9['农村'].replace(6, '在', inplace = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f998f23",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_9['抖音'].replace('不在', '没有', inplace = True)\n",
    "df_9['抖音'].replace('在', '有', inplace = True)\n",
    "df_9['王者荣耀'].replace('不在', '没有', inplace = True)\n",
    "df_9['王者荣耀'].replace('在', '有', inplace = True)\n",
    "df_9['腾讯视频'].replace('不在', '没有', inplace = True)\n",
    "df_9['腾讯视频'].replace('在', '有', inplace = True)\n",
    "df_9['百度'].replace('不在', '没有', inplace = True)\n",
    "df_9['百度'].replace('在', '有', inplace = True)\n",
    "df_9['爱奇艺'].replace('不在', '没有', inplace = True)\n",
    "df_9['爱奇艺'].replace('在', '有', inplace = True)\n",
    "df_9['优酷'].replace('不在', '没有', inplace = True)\n",
    "df_9['优酷'].replace('在', '有', inplace = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "05fc1895",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_9['高校'].replace(-1, '不在', inplace = True)\n",
    "df_9['高校'].replace(3, '在', inplace = True)\n",
    "\n",
    "df_9['全部游戏都卡顿'].replace(-1, '否', inplace = True)\n",
    "df_9['全部游戏都卡顿'].replace(99, '是', inplace = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64f51485",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_9['和平精英'].replace('不在', '没有', inplace = True)\n",
    "df_9['和平精英'].replace('在', '有', inplace = True)\n",
    "df_9['新浪微博'].replace('不在', '没有', inplace = True)\n",
    "df_9['新浪微博'].replace('在', '有', inplace = True)\n",
    "df_9['手机QQ'].replace('不在', '没有', inplace = True)\n",
    "df_9['手机QQ'].replace('在', '有', inplace = True)\n",
    "df_9['今日头条'].replace('不在', '没有', inplace = True)\n",
    "df_9['今日头条'].replace('在', '有', inplace = True)\n",
    "df_9['其他，请注明'].replace(-1, '没有', inplace = True)\n",
    "df_9['其他，请注明'].replace(98, '有', inplace = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1792410b",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_9 = pd.get_dummies(df_9)\n",
    "df_10 = pd.concat([data_4.iloc[:,0], df_9], axis = 1)\n",
    "df_10 = pd.get_dummies(df_10)\n",
    "x = df_10.iloc[:, 1 : ]\n",
    "y = df_10.iloc[:, 0]\n",
    "smote_model = SMOTE()\n",
    "x_smote, y_smote = smote_model.fit_resample(x, y)\n",
    "smote_df = pd.concat([y_smote, x_smote], axis=1)\n",
    "df = smote_df.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5ea9aaad",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "model = OneVsRestClassifier(RandomForestClassifier(random_state = 10))\n",
    "model.fit(X_train, y_train)\n",
    "y_pred = model.predict(X_test)\n",
    "print(classification_report(y_test, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b89ddaa5",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "model_1 = OneVsRestClassifier(XGBClassifier(n_estimators = 300, max_depth = 5, random_state = 10))\n",
    "model_1.fit(X_train, y_train)\n",
    "y_pred = model_1.predict(X_test)\n",
    "print(classification_report(y_test, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f66b48da",
   "metadata": {},
   "outputs": [],
   "source": [
    "y_pred_1 = model_1.predict(df_8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5292c28",
   "metadata": {},
   "outputs": [],
   "source": [
    "pred_data = pd.read_excel('./result.xlsx', sheet_name = '上网')\n",
    "pred_data['手机上网整体满意度'] = y_pred_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75d1ca9c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 网络覆盖\n",
    "df_11 = pd.concat([data_4.iloc[:, 1], df_9], axis = 1)\n",
    "df_11 = pd.get_dummies(df_11)\n",
    "x = df_11.iloc[:, 1 : ]\n",
    "y = df_11.iloc[:, 0]\n",
    "smote_model = SMOTE()\n",
    "x_smote, y_smote = smote_model.fit_resample(x, y)\n",
    "smote_df = pd.concat([y_smote, x_smote], axis=1)\n",
    "df = smote_df.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a304960",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "model = OneVsRestClassifier(RandomForestClassifier(random_state = 10))\n",
    "model.fit(X_train, y_train)\n",
    "y_pred = model.predict(X_test)\n",
    "print(classification_report(y_test, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0937ee3a",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "model_1 = OneVsRestClassifier(XGBClassifier(n_estimators = 300, max_depth = 5, random_state = 10))\n",
    "model_1.fit(X_train, y_train)\n",
    "y_pred = model_1.predict(X_test)\n",
    "print(classification_report(y_test, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "70787c8d",
   "metadata": {},
   "outputs": [],
   "source": [
    "y_pred_2 = model_1.predict(df_8)\n",
    "pred_data['网络覆盖与信号强度'] = y_pred_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "738b1a6b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 手机上网速度\n",
    "df_12 = pd.concat([data_4.iloc[:, 2], df_9], axis = 1)\n",
    "df_12 = pd.get_dummies(df_12)\n",
    "x = df_12.iloc[:, 1 : ]\n",
    "y = df_12.iloc[:, 0]\n",
    "smote_model = SMOTE()\n",
    "x_smote, y_smote = smote_model.fit_resample(x, y)\n",
    "smote_df = pd.concat([y_smote, x_smote], axis=1)\n",
    "df = smote_df.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "665cb442",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "model = OneVsRestClassifier(RandomForestClassifier(random_state = 10))\n",
    "model.fit(X_train, y_train)\n",
    "y_pred = model.predict(X_test)\n",
    "print(classification_report(y_test, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28108262",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "model_1 = OneVsRestClassifier(XGBClassifier(n_estimators = 300, max_depth = 5, random_state = 10))\n",
    "model_1.fit(X_train, y_train)\n",
    "y_pred = model_1.predict(X_test)\n",
    "print(classification_report(y_test, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a7749f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "y_pred_3 = model_1.predict(df_8)\n",
    "pred_data['手机上网速度'] = y_pred_3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b9868a56",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 手机上网稳定性\n",
    "from imblearn.over_sampling import SMOTE\n",
    "df_13 = pd.concat([data_4.iloc[:, 3], df_9], axis = 1)\n",
    "df_13 = pd.get_dummies(df_13)\n",
    "x = df_13.iloc[:, 1 : ]\n",
    "y = df_13.iloc[:, 0]\n",
    "smote_model = SMOTE()\n",
    "x_smote, y_smote = smote_model.fit_resample(x, y)\n",
    "smote_df = pd.concat([y_smote, x_smote], axis=1)\n",
    "df = smote_df.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1da07ac1",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "model = OneVsRestClassifier(RandomForestClassifier(random_state = 10))\n",
    "model.fit(X_train, y_train)\n",
    "y_pred = model.predict(X_test)\n",
    "print(classification_report(y_test, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f81cf788",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "model_1 = OneVsRestClassifier(XGBClassifier(n_estimators = 300, max_depth = 5, random_state = 10))\n",
    "model_1.fit(X_train, y_train)\n",
    "y_pred = model_1.predict(X_test)\n",
    "print(classification_report(y_test, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "548ab56e",
   "metadata": {},
   "outputs": [],
   "source": [
    "y_pred_4 = model_1.predict(df_8)\n",
    "pred_data['手机上网稳定性'] = y_pred_4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac30d05a",
   "metadata": {},
   "outputs": [],
   "source": [
    "pred_data.to_excel('./data/predict2.xlsx')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
